/**
 * 搜索设备界面
 */
Page({
  data: {
    list: [],
    isPlay:false,
    song:{},
    arr:{},
    musicId:'',
    poster:'',
    name:'',
    src:''
  },
  onPullDownRefresh: function () {
    wx.request({
      url: 'https://jingqu.kuxia.top/app/Explain/index',
      data: {
        scenic_id: 3
      },
      method: 'POST',
      success: (res) => {
        console.log(res);
        let that = this
        let BeaconData = res.data.data
        that.setData({
          list: BeaconData,
          arr:res.data
        })
        let seach = {}
        let uuids = []
        for (let i = 0; i < res.data.data.length; i++) {
          uuids.push(res.data.data[i].deviceid)
        }
        console.log(uuids);
        // 初始化蓝牙设备
        wx.openBluetoothAdapter({
          success: function (blueres) {
            console.log('蓝牙适配器初始化成功', blueres)
            wx.startBeaconDiscovery({
              uuids: uuids, //多个uuid数组
              success: function (res) {
                console.log(res);
                wx.onBeaconUpdate(function () {
                  //设置监听事件
                  wx.getBeacons({
                    //在监听事件中获取数据
                    success: res => {
                      console.log(res.beacons) //res.beacons 为搜索到的iBeacon数据数组
                      //业务代码
                      for (let i = 0; i < BeaconData.length; i++) {
                        console.log(res.beacons[0].uuid);
                        console.log(BeaconData[i].deviceid);
                        if (res.beacons[0].uuid.toUpperCase() == BeaconData[i].deviceid.toUpperCase()) {
                          console.log(123);
                          that.setData({
                            list: BeaconData
                          })
                        }
                      }
                      console.log(seach);
                      wx.stopBeaconDiscovery({
                        success(res) {
                          console.log("已暂停搜索", res);
                        }
                      })
                    }
                  })
                })
              }
            })
          },
          fail: function (res) {
            console.log(res);
            wx.showModal({
              title: '提醒',
              content: '系统监测未打开蓝牙，请打开后下拉刷新',
            })
            console.log(uuids);
          }
        })
      }
    })
    wx.stopPullDownRefresh()  //自动关闭下拉刷新
  },
  onLoad: function () {
   
      wx.request({
        url: 'https://jingqu.kuxia.top/app/Explain/index',
        data: {
          scenic_id: 3
        },
        method: 'POST',
        success: (res) => {
          console.log(res);
          this.setData({
            arr: res.data
          });
          let that = this;
          let BeaconData = res.data.data;
          let uuids = [];
          for (let i = 0; i < BeaconData.length; i++) {
            let deviceId = BeaconData[i].deviceid;
            if(/^[A-F0-9\-]{36}$/.test(deviceId)){
              uuids.push(deviceId)
            }else{
              console.log(`无效的:${deviceId}`)
            }
          }
          console.log(uuids);
          // 初始化蓝牙设备
          wx.openBluetoothAdapter({
            success: function (blueres) {
              console.log('蓝牙适配器初始化成功', blueres)
              wx.startBeaconDiscovery({
                uuids: uuids, //多个uuid数组
                success: function (res) {
                  console.log(res);
                  wx.onBeaconUpdate(function () {
                    //设置监听事件
                    wx.getBeacons({
                      //在监听事件中获取数据
                      success: res => {
                        console.log(res.beacons) //res.beacons 为搜索到的iBeacon数据数组
                        //业务代码
                        for (let i = 0; i < BeaconData.length; i++) {
                          console.log(res.beacons[0].uuid);
                          console.log(BeaconData[i].deviceid);
                          if (res.beacons[0].uuid.toUpperCase() == BeaconData[i].deviceid.toUpperCase()) {
                            console.log(123);
                            that.setData({
                              list: BeaconData
                            });
                            that.startMusic();
                          }
                        }
                        wx.stopBeaconDiscovery({
                          success(res) {
                            console.log("已暂停搜索", res);
                          }
                        })
                      }
                    })
                  })
                }
              })
            },
            fail: function (res) {
              console.log(res);
              wx.showModal({
                title: '提醒',
                content: '系统监测未打开蓝牙，请打开后下拉刷新',
              })
              console.log(uuids);
            }
          })
        }
      })
    },
    // 修改播放状态的功能函数
    startMusic: function () {
      wx.playBackgroundAudio({
        dataUrl: `https://jingqu.kuxia.top${arr.data.data[0].audio}`,
        title:'李',
        success: () =>{
          console.log('播放成功')
        },
        fail:(err)=>{
          console.log('播放失败：',err)
        }
      })
      
    // 修改音乐是否的状态
  },
 
  // 获取音乐详情的功能函数
  async getMusicInfo(musicId) {
    let songData = await request('/song/detail', { ids: musicId });
 
    this.setData({
      song: songData.songs,
    })
  },
 

 
  // 控制音乐播放/暂停的功能函数
})